﻿Public Class ThemHanhKhach
    Private bus_ThemHK As New BUS.ThemHanhKhach
    Private dto_ThemHK As New DTO.ThemHanhKhach
    Private bustracuuhk As New BUS.TraCuuKH
    Private dtotracuuhk As New DTO.TraCuuHK
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Luu()
    End Sub

    Private Sub Luu()
        Dim n As Integer = 0
        If txtcmnd.Text = "" Or Not Integer.TryParse(txtcmnd.Text, n) Then
            MessageBox.Show("Chứng minh thư nhân dân khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If
        If txthoten.Text = "" Then
            MessageBox.Show("Họ tên khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If
        If txtdienthoai.Text = "" Or Not Integer.TryParse(txtdienthoai.Text, n) Then
            MessageBox.Show("Số điện thoại khách hàng không đúng!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If
        If bus_ThemHK.CheckHK(txtcmnd.Text) = False Then
            MessageBox.Show("Khách hàng này đã tồn tại!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If

        dto_ThemHK.cmnd = txtcmnd.Text
        dto_ThemHK.hoten = txthoten.Text
        dto_ThemHK.dienthoai = txtdienthoai.Text
        bus_ThemHK.save(dto_ThemHK)
        MessageBox.Show("Thành công!", "Thông báo", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation)
        clall()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Hide()
    End Sub

    Private Sub clall()
        txtcmnd.Text = ""
        txtdienthoai.Text = ""
        txthoten.Text = ""
    End Sub

    Private Sub ThemHanhKhach_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Hide()
        e.Cancel = True
        If Not Main.f_tracuuhk Is Nothing Then
            Main.f_tracuuhk.dgkhachhang.DataSource = bustracuuhk.dtload().Tables(0)
        End If
    End Sub

    Private Sub ThemHanhKhach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class